// pages/appointment/appointmentList/appointmentList.js
import { API_HOST } from "../../../utils/const"
import http from "../../../utils/http"
import util from "../../../utils/util"

Page({

  /**
   * 页面的初始数据
   */
  data: {
    host: API_HOST,
    // 没有登录出现
    appointment_isLogin: false,
    //判断有没有数据
    appointment_isList: false,
    //判断房源的文字状态
    success_kan_text: false,
    xia_kan_text: false,
    kan_text: true,
    //切換显示状态
    task_list: true,
    //请求来的数据状态
    yueTab: false,
    token: '',
    //每页条数
    pageSize: 10,
    //未頁碼
    nopageNum: 1,
    //已完成页码
    pageNum: 1,
    //未数据
    notaskData: [],
    //已完成
    taskData: [],
    //总共多页
    allPage: ''

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(util);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onShow: function () {
    let isLoginStatus = wx.getStorageSync("isLoginStatus") || false;
    let phoneNum = wx.getStorageSync("phoneNum") || 0;
    let token = wx.getStorageSync("token") || null;
    let that = this;
    this.setData({
      appointment_isLogin: !isLoginStatus
    })
    if (isLoginStatus && token) {
      this.setData({
        notaskData: [],
        taskData: [],
        nopageNum: 1,
        pageNum: 1
      })
      this.data.task_list ? that.getDataList(0, that.data.nopageNum, that.data.pageSize) : that.getDataList(1, that.data.pageNum, that.data.pageSize);
    } else {
      this.setData({
        notaskData: [],
        taskData: [],
        nopageNum: 1,
        pageNum: 1,
        appointment_isList: false,
        yueTab: false
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  },
  /**
   * 下拉加载动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading()
    if (this.data.task_list) {
      this.getDataList(0, this.data.nopageNum, this.data.pageSize);
    } else {
      this.getDataList(1, this.data.pageNum, this.data.pageSize);
    }
  },
  /**
   * 上拉加载动作
   */
  onReachBottom: function () {
    if (this.data.task_list) {
      if (this.data.nopageNum <= this.data.allPage) {
        this.setData({
          nopageNum: this.data.nopageNum + 1
        });
        this.getDataList(0, this.data.nopageNum, this.data.pageSize);
      } else {
        this.setData({
          nopageNum: this.data.allPage
        });
        wx.showToast({
          icon: "none",
          title: "已经是底线了"
        })
      }
    } else {
      if (this.data.pageNum <= this.data.allPage) {
        this.setData({
          pageNum: this.data.pageNum + 1
        });
        this.getDataList(1, this.data.pageNum, this.data.pageSize);
      } else {
        this.setData({
          pageNum: this.data.allPage
        });
        wx.showToast({
          icon: "none",
          title: "已经是底线了"
        })
      }
    }

  },
  //完成约看列表的状态
  no_task_list: function () {
    this.setData({
      task_list: true,
      nopageNum: 1,
      notaskData: []
    });
    this.getDataList(0, this.data.nopageNum, this.data.pageSize);
  },
  success_task_list: function () {
    this.setData({
      task_list: false,
      pageNum: 1,
      taskData: []
    });
    this.getDataList(1, this.data.pageNum, this.data.pageSize);
  },
  //去登录
  goLogin: function () {
    wx.navigateTo({
      url: '../../my/login/login?url=/pages/appointment/appointmentList/appointmentList'
    })
  },
  //删除
  itemDel: function (val) {
    if (this.data.task_list) {
      let num = this.delNum(this.data.notaskData, val.detail.id);
      this.data.notaskData.splice(num, 1);
      this.setData({
        notaskData: this.data.notaskData
      })
    } else {
      let num = this.delNum(this.data.taskData, val.detail.id);
      this.data.taskData.splice(num, 1);
      this.setData({
        taskData: this.data.taskData
      })
    }
  },
  //完成看房更新
  itemSuccess: function (val) {
    if (this.data.task_list) {
      let num = this.delNum(this.data.notaskData, val.detail.id);
      this.data.notaskData.splice(num, 1);
      this.setData({
        notaskData: this.data.notaskData
      })
    } else {
      let num = this.delNum(this.data.taskData, val.detail.id);
      this.data.taskData.splice(num, 1);
      this.setData({
        taskData: this.data.taskData
      })
    }
  },
  //初始化数据
  setDataList: function (data) {
    var that = this;
    var arr = [];
    if (that.data.task_list) {
      data.forEach(function (val) {
        val.startTime = util.getMonthDay(val.startTime);
        val.processPulishTime = util.getMonthDay(val.processPulishTime);
        val.txtStyle = '';
        if (that.duparr(val, that.data.notaskData)) {
          arr.push(val);
        }
      });
      arr.forEach(function (val) {
        that.data.notaskData.push(val);
      })
      that.setData({
        notaskData: that.data.notaskData
      });
    } else {
      data.forEach(function (val) {
        val.startTime = util.getMonthDay(val.startTime);
        val.processPulishTime = util.getMonthDay(val.processPulishTime);
        val.txtStyle = '';
        if (that.duparr(val, that.data.taskData)) {
          arr.push(val);
        }
      });
      arr.forEach(function(val){
        that.data.taskData.push(val);
      })
      that.setData({
        taskData: that.data.taskData
      });
    }

  },
  //请求接口
  getDataList: function (statusNum, pageNum, pageSize) {
    var that = this;
    http({
      url: "/look/queryReservationList/v1.0.0",
      method: "POST",
      data: {
        status: statusNum,
        pageNum: pageNum,
        pageSize: pageSize
      },
      success: function (res) {
        if (res.code == 200) {
          that.setData({
            allPage: res.data.totalPage
          })
          if (res.code == 200 && res.data.rowItemList.length) {
            that.setData({
              appointment_isList: false,
              yueTab: true
            })
            that.setDataList(res.data.rowItemList);
          } else {
            that.setData({
              appointment_isList: true,
              yueTab: true,
              taskData: []
            })
          }
        } else if (res.code == 403) {
          wx.showModal({
            title: "提示",
            showCancel: false,
            content: res.msg,
            success: function (res) {
              if (res.confirm) {
                wx.clearStorageSync("token")
                wx.clearStorageSync("isLoginStatus");
                wx.clearStorageSync("phoneNum");
                wx.navigateTo({
                  url: '../../my/login/login?url=/pages/appointment/appointmentList/appointmentList'
                })
              }
            }
          })
        } else if (res.code == 500) {
          wx.showModal({
            title: "提示",
            showCancel: false,
            content: res.msg,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../../my/login/login?url=/pages/appointment/appointmentList/appointmentList'
                })
              }
            }
          })
        } else {
          console.log("约看列表请求出错");
        }
      }
    })
  },
  //数据去才重复
  duparr: function (target, arr) {
    let isOff = true;
    for (let i = 0; i < arr.length; i++) {
      if (target.id == arr[i].id) {
        isOff = false;
        break;
      }
    }
    return isOff;
  },
  //选择下标
  delNum: function (data, target) {
    var num = '';
    data.forEach(function (val, index) {
      if (val.id == target) {
        num = index;
      }
    })
    return num;
  }
})